package com.sky.Task;


import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class processTimeoutOrder {
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 处理超时订单
     */
    @Scheduled(cron = "0 * * * * ? ")//每一分钟处理一次
    public void processTimeoutOrder(){
        log.info("时间：{}", LocalDateTime.now());
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-15);
        List<Orders> byStatusAndOrdertimeLT = ordersMapper.getByStatusAndOrdertimeLT(Orders.PENDING_PAYMENT, localDateTime);
        if (byStatusAndOrdertimeLT!=null && byStatusAndOrdertimeLT.size()>0){
            for (Orders orders : byStatusAndOrdertimeLT) {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelReason("支付时间超时");
                orders.setCancelTime(LocalDateTime.now());
                log.info("订单以取消");
            }
        }
    }

    @Scheduled(cron = "0 0 1 * * ?")//每天处理一次
    public void processDeliveryOrder(){
        log.info("时间：{}", LocalDateTime.now());
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-60);
        List<Orders> byStatusAndOrdertimeLT = ordersMapper.getByStatusAndOrdertimeLT(Orders.DELIVERY_IN_PROGRESS, localDateTime);
        if (byStatusAndOrdertimeLT !=null || byStatusAndOrdertimeLT.size()>0){
            for (Orders orders : byStatusAndOrdertimeLT) {
                orders.setStatus(Orders.COMPLETED);
            }
        }
    }
}
